#include <stdio.h>
#include <stdlib.h>

//function prototype: generateRandString 
void generateRandString(const int size, char *buffer);

//function prototype: generateRandIntArray 
void generateRandIntArray(const int size, int *buffer);

//function main
int main(int argc, char **argv){
  //declare local integer variables i, n and j
  int i, n, j;
  //declare local pointer to char variable buffer
  //buffer can be seen as an array of characters
  char *buffer;
  //declare local pointer to int variable bufferInt
  int *bufferInt; 

  //generate random String
  //print text "How long do you want the string? "
  printf ("How long do you want the string? ");
  //read from the standard input variable i, using an integer format
  scanf("%d", &i);

  //allocate memory for variable buffer to hold i+1 characters
  buffer = (char*) malloc (i+1);

  //if no memory was allocated (buffer variable holds a value of 0 or NULL) 
  if (buffer == NULL)
  exit (1);   //then exit, returning 1

  generateRandString(i, buffer);

  //print the buffer variable using a string format (since last character is NULL) 
  printf ("Random string: %s\n",buffer);

  //free(release) memory allocated to pointer variable buffer
  free (buffer);

  //generate random Integer array
  //print text "How long do you want the integer array? "
  printf ("How long do you want the integer array? ");
  //read from the standard input variable j, using an integer format
  scanf("%d", &j);

  //allocate memory for variable bufferInt to hold j integers
  bufferInt = (int*) malloc (j * sizeof(int));

  //if no memory was allocated (bufferInt variable holds a value of 0 or NULL) 
  if (bufferInt == NULL)
  exit (1);   //then exit, returning 1

  generateRandIntArray(j, bufferInt);

  //print the bufferInt array variable
  printf ("Random integer array: ");
  for(n=0; n<j; n++){
    printf("%d ", bufferInt[n]);
  }
  printf("\n");

  //free(release) memory allocated to pointer variable buffer
  free (bufferInt);

  //exit the program returning 0 
  return 0;
}

//function generateRandString
void generateRandString(const int size, char *buffer)
{
  int n;

  //for loop where variable n starts with 0 and goes to size-1
  for (n=0; n<size; n++) {
    //set character at position n of buffer variable to a random value that is calculated in this way:
    // rand() returns a random integer number greter or equal with 0
    // modulo operation is applied to the random integer number and as a result the integer number will be between 0 and 25
    // the integer is then added to 'a' and the result will be character between 'a' (when the integer is 0) and 'z' (when the integer is 25) 	
    buffer[n] = rand() % 26 +'a';
  }
  //last character of the buffer array of characters is set to character NULL ('\0')
  buffer[size]='\0';
}

//function generateRandIntArray
void generateRandIntArray(const int size, int *buffer)
{
  int n;

  //for loop where variable n starts with 0 and goes to size-1
  for (n=0; n<size; n++) {
    //set int at position n of buffer variable to a random value that is calculated in this way:
    // rand() returns a random integer number greter or equal with 0
    // modulo operation is applied to the random integer number and as a result the integer number will be between 0 and 25
    // the integer is then added to 'a' and the result will be an integer between 97 (when the integer is 0) and 122 (when the integer is 25) 	
    buffer[n] = rand() % 26 +'a';
  }
}

